home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / ARMTEX / SOURCES1 / !TeX / texmf / source / armTeX / lib / c / openinout < prev    next >
Encoding:
Text File  |  1998-04-06  |  6.9 KB  |  274 lines

  1. /* openinout.c: open input and output files.  These routines used by
  2.    TeX, Metafont, and BibTeX.  */
  3.  
  4. #include "config.h"
  5. #include "c-namemx.h"
  6. #include "c-pathch.h"
  7.  
  8. #ifdef RISCOS
  9. #include "riscos_ex.h"
  10. #endif
  11.  
  12. #ifdef BibTeX
  13. /* See comments in bibtex.ch for why we need these.  */
  14. FILE *standardinput = stdin;
  15. FILE *standardoutput = stdout;
  16.  
  17. /* Because we don't generate a .h file with all the global definitions
  18.    for BibTeX, as we do with TeX and Metafont, we must declare these
  19.    variables.  */
  20. extern char nameoffile[];
  21. extern integer namelength;
  22.  
  23. #else /* not BibTeX */
  24.  
  25. #define EXTERN extern        /* Don't instantiate data here.  */
  26.  
  27. #ifdef TeX
  28. #include "texd.h"
  29. #else /* Metafont */
  30. #include "mfd.h"
  31. #endif
  32.  
  33. #ifdef FUNNY_CORE_DUMP
  34. /* This is defined in ./texmf.c.  */
  35. extern void funny_core_dump ();
  36. #endif /* FUNNY_CORE_DUMP */
  37.  
  38. #endif /* not BibTeX */
  39.  
  40. /* Open an input file F, using the path PATHSPEC and passing
  41.    FOPEN_MODE to fopen.  The filename is in `nameoffile', as a Pascal
  42.    string. We return whether or not the open succeeded.  If it did, we
  43.    also set `namelength' to the length of the full pathname that we
  44.    opened.  */
  45.  
  46. boolean
  47. open_input (f, path_index, fopen_mode)
  48.      FILE **f;
  49.      path_constant_type path_index;
  50.      char *fopen_mode;
  51. {
  52.   boolean openable = false;
  53.  
  54. #if defined (FUNNY_CORE_DUMP) && !defined (BibTeX)
  55.   /* This only applies if a preloaded TeX (or Metafont) is being made;
  56.      it allows for automatic creation of the core dump (typing ^\
  57.      requires manual intervention).  */
  58.   if (path_index == TEXINPUTPATH
  59.   && strncmp (nameoffile + 1, "HackyInputFileNameForCoreDump.tex", 33) == 0)
  60.     funny_core_dump ();
  61. #endif /* FUNNY_CORE_DUMP and not BibTeX */
  62.  
  63. #ifdef BibTeX
  64.   if (path_index == NO_FILE_PATH)
  65.     {
  66.       unsigned temp_length;
  67.  
  68.       null_terminate (nameoffile + 1);
  69.       *f = riscos_fopen (nameoffile + 1, fopen_mode);
  70.       temp_length = strlen (nameoffile + 1);
  71.       space_terminate (nameoffile + 1);
  72.       if (*f != NULL)
  73.     {
  74.       namelength = temp_length;
  75.       openable = true;
  76.     }
  77.     }
  78.  
  79.   else
  80. #endif /* BibTeX */
  81.  
  82.   if (testreadaccess (nameoffile, path_index))
  83.     {
  84.       /* We can assume `nameoffile' is openable, since
  85.          `testreadaccess' just returned true.  */
  86.       *f = xfopen_pas (nameoffile, fopen_mode);
  87.  
  88.       /* If we found the file in the current directory, don't leave the
  89.          `./' at the beginning of `nameoffile', since it looks dumb when
  90.          TeX says `(./foo.tex ... )', and analogously for Metafont.  */
  91.       namelength = strchr (nameoffile + 1, ' ') - nameoffile - 1;
  92. #ifdef RISCOS
  93.       if (NULL == strncmp (nameoffile + 1, current_prefix,
  94.                current_prefix_length))
  95.     {
  96.       memmove (nameoffile + 1, nameoffile + current_prefix_length + 1,
  97.            namelength - current_prefix_length + 1);
  98.       namelength -= current_prefix_length;
  99.     }
  100. #endif /* not RISCOS */
  101.       if (nameoffile[1] == '@' && nameoffile[2] == PATH_SEP)
  102.     {
  103.       memmove (nameoffile + 1, nameoffile + 3, namelength - 1);
  104.       namelength -= 2;
  105.     }
  106.  
  107. #ifdef TeX
  108.       /* If we just opened a TFM file, we have to read the first byte,
  109.          since TeX wants to look at it.  What a kludge.  */
  110.       if (path_index == TFMFILEPATH)
  111.     {            /* See comments in ctex.ch for why we need this.  */
  112.       extern integer tfmtemp;
  113.       tfmtemp = getc (*f);
  114.     }
  115. #endif /* TeX */
  116.  
  117.       openable = true;
  118.     }
  119.  
  120.   return openable;
  121. }
  122.  
  123.  
  124. /* Call the external program PROGRAM, passing it `nameoffile'.  */
  125.  
  126. static boolean
  127. make_tex_file (program)
  128.      string program;
  129. {
  130. #ifdef NO_MAKETEX
  131.   return 0;
  132. #else
  133.   char cmd[NAME_MAX + 1 + PATH_MAX + 1];
  134.   unsigned cmd_len;
  135.   int ret;
  136.   unsigned i = 1;        /* For copying from `nameoffile'.  */
  137.  
  138.   /* Wrap another sh around the invocation of the MakeTeX program, so we
  139.      can avoid `sh: MakeTeXTFM: not found' errors confusing the user.
  140.      We don't use fork/exec ourselves, since we'd have to call sh anyway
  141.      to interpret the script.  */
  142.  
  143.   /* We don't do that in RISC OS cause there's no need */
  144. #ifndef RISCOS
  145.   strcpy (cmd, "sh -c ");
  146.  
  147.   strcat (cmd, program);
  148. #else /* RISCOS */
  149.   strcpy (cmd, program);
  150. #endif /* RISCOS */
  151.   cmd_len = strlen (cmd);
  152.   cmd[cmd_len++] = ' ';
  153.  
  154.   while (nameoffile[i] != ' ')
  155.     cmd[cmd_len++] = nameoffile[i++];
  156.  
  157.   /* Add terminating null.  */
  158.   cmd[cmd_len] = 0;
  159.  
  160.   /* Don't show any output.  */
  161.   /* RISC OS: We trust our users not to be confused */
  162. #ifndef RISCOS
  163.   strcat (cmd, ">/dev/null 2>1");
  164. #endif /* not RISCOS */
  165.  
  166.   /* Run the command, and return whether or not it succeeded.  */
  167.   ret = system (cmd);
  168.   return ret == EXIT_SUCCESS_CODE;
  169. #endif /* not NO_MAKE_TEX */
  170. }
  171.  
  172.  
  173. /* This is called by TeX if an \input resp. TFM file can't be opened.  */
  174.  
  175. boolean
  176. maketextex ()
  177. {
  178.   return make_tex_file ("MakeTeXTeX");
  179. }
  180.  
  181. boolean
  182. maketextfm ()
  183. {
  184.   return make_tex_file ("MakeTeXTFM");
  185. }
  186.  
  187. boolean
  188. maketexmf ()
  189. {
  190.   return make_tex_file ("MakeTeXMF");
  191. }
  192.  
  193. /* Open an output file F either in the current directory or in
  194.    $TEXMFOUTPUT/F, if the environment variable `TEXMFOUTPUT' exists.
  195.    (Actually, this applies to the BibTeX output files, also, but
  196.    `TEXMFBIBOUTPUT' was just too long.)  The filename is in the global
  197.    `nameoffile', as a Pascal string.  We return whether or not the open
  198.    succeeded.  If it did, the global `namelength' is set to the length
  199.    of the actual filename.
  200.    For RISC OS the name of this variable is changed to TeXMFOutput$Dir
  201.    to have a consistent name  */
  202.  
  203. boolean
  204. open_output (f, fopen_mode)
  205.      FILE **f;
  206.      char *fopen_mode;
  207. {
  208.   unsigned temp_length;
  209.  
  210.   /* Make the filename into a C string.  */
  211.   null_terminate (nameoffile + 1);
  212.  
  213.   /* Is the filename openable as given?  */
  214.   *f = riscos_fopen (nameoffile + 1, fopen_mode);
  215.  
  216.   if (*f == NULL)
  217.     {                /* Can't open as given.  Try the envvar.  */
  218. #ifdef RISCOS
  219.       string temp_dir = getenv ("TeXMFOutput$Dir");
  220. #else
  221.       string temp_dir = getenv ("TEXMFOUTPUT");
  222. #endif
  223.  
  224.       if (temp_dir != NULL)
  225.     {
  226.       /* 29-Apr-93: Changed literal "/" to PATH_SEP_STRING (marks) */
  227.       string temp_name = concat3 (temp_dir, PATH_SEP_STRING, nameoffile + 1);
  228.       *f = fopen (temp_name, fopen_mode);
  229.  
  230.       /* If this succeeded, change nameoffile accordingly.  */
  231.       if (*f)
  232.         strcpy (nameoffile + 1, temp_name);
  233.  
  234.       free (temp_name);
  235.     }
  236.     }
  237.  
  238.   /* Back into a Pascal string, but first get its length.  */
  239.   temp_length = strlen (nameoffile + 1);
  240.   space_terminate (nameoffile + 1);
  241.  
  242.   /* Only set `namelength' if we succeeded.  I'm not sure why.  */
  243.   if (*f)
  244.     namelength = temp_length;
  245.  
  246.   return *f != NULL;
  247. }
  248.  
  249. /* Test if the Pascal string BASE concatenated with the extension
  250.    `.SUFFIX' is the same file as just BASE.  SUFFIX is a C string.  */
  251.  
  252. boolean
  253. extensionirrelevantp (base, suffix)
  254.      char *base;
  255.      char *suffix;
  256. {
  257. #ifdef RISCOS            /* can't happen using the chosen scheme */
  258.   return false;
  259. #else
  260.   boolean ret;
  261.   char temp[PATH_MAX];
  262.  
  263.   make_c_string (&base);
  264.   strcpy (temp, base);
  265.   strcat (temp, ".");
  266.   strcat (temp, suffix);
  267.  
  268.   ret = same_file_p (base, temp);
  269.   make_pascal_string (&base);
  270.  
  271.   return ret;
  272. #endif
  273. }
  274.